package toolx

import (
	"fmt"
	"runtime"

	"gitee.com/workits/pkgs/configx"
	"gitee.com/workits/pkgs/utilx"
)

func NewProject(projName, comp string) {
	// 错误处理
	defer func() {
		if err := recover(); err != nil {
			utilx.FsRemove(projName, true)
			panic(err)
		}
	}()

	// 创建项目环境
	CopyTemplates(projName, false)
	// 创建配置文件
	CreateConfig(projName)

	// 读取配置文件
	var wkcfg WkCfg
	if _, err := configx.UnmarshalFiles(projName+PS+".workits", &wkcfg); err != nil {
		panic("🙅配置文件读取失败")
	}

	// 构造参数
	params := make(map[string]any)
	params["GoModule"] = wkcfg.GoModule
	params["GoVersion"] = runtime.Version()[2:]
	params["ProjectName"] = projName
	params["Dialect"] = wkcfg.DB.Dialect
	params["Driver"] = wkcfg.DB.Driver
	params["DSN"] = wkcfg.DB.DSN()

	// 生成项目骨架
	// ---- 各级目录
	dirs := []string{
		"component",
		"config",
		"internal" + PS + "app",
		"internal" + PS + "component",
		"internal" + PS + "config",
	}
	for _, dir := range dirs {
		if err := utilx.CreateMutiDir(projName + PS + dir); err != nil {
			panic("🙅创建项目目录失败")
		}
	}

	// 项目缓存模版路径
	projCacheTplDir := projName + PS + ".workits" + PS + "templates"
	// ---- 生成文件
	if err := utilx.CopyFile(projCacheTplDir+PS+"component_component.go.tpl", projName+PS+"component"+PS+"component.go", false); err != nil {
		panic("🙅生成component/component.go失败")
	}
	// config/application.yaml
	ParseTemplate(projCacheTplDir+PS+"config_application.yaml.tpl", projName+PS+"config"+PS+"application.yaml", params)
	if err := utilx.CopyFile(projCacheTplDir+PS+"gitignore.tpl", projName+PS+".gitignore", false); err != nil {
		panic("🙅生成.gitignore失败")
	}
	// go.mod
	ParseTemplate(projCacheTplDir+PS+"go.mod.tpl", projName+PS+"go.mod", params)
	if err := utilx.CreateFile(projName+PS+"internal"+PS+"app"+PS+".gitkeep", nil); err != nil {
		panic("🙅生成internal/app/.gitkeep失败")
	}
	if err := utilx.CreateFile(projName+PS+"internal"+PS+"component"+PS+".gitkeep", nil); err != nil {
		panic("🙅生成internal/component/.gitkeep失败")
	}
	if err := utilx.CopyFile(projCacheTplDir+PS+"internal_config_application_config.go.tpl", projName+PS+"internal"+PS+"config"+PS+"application_config.go", false); err != nil {
		panic("🙅生成internal/config/application_config.go失败")
	}
	if err := utilx.CopyFile(projCacheTplDir+PS+"internal_config_global.go.tpl", projName+PS+"internal"+PS+"config"+PS+"global.go", false); err != nil {
		panic("🙅生成internal/config/global.go失败")
	}
	// main.go
	ParseTemplate(projCacheTplDir+PS+"main.go.tpl", projName+PS+"main.go", params)

	// 创建模块
	NewComponent(projName, comp)

	// 创建demo项目
	NewModule(projName, "demo")
	// 生成demo项目文件
	if err := utilx.CopyFile(projCacheTplDir+PS+"hello_model.go.tpl", projName+PS+"internal"+PS+"app"+PS+"demo"+PS+"model"+PS+"hello_model.go", false); err != nil {
		panic("🙅生成internal/app/demo/model/hello_model.go失败")
	}
	// internal/app/demo/handler/hello_handler.go
	ParseTemplate(projCacheTplDir+PS+"hello_handler.go.tpl", projName+PS+"internal"+PS+"app"+PS+"demo"+PS+"handler"+PS+"hello_handler.go", params)
	// internal/app/demo/component/router/hello_router.go
	ParseTemplate(projCacheTplDir+PS+"hello_router.go.tpl", projName+PS+"internal"+PS+"app"+PS+"demo"+PS+"component"+PS+"router"+PS+"hello_router.go", params)

	// 打印结束语
	fmt.Println(`
🎉🎉🎉 项目创建成功！接下来就可以开始愉快的撸代码了:

在项目根目录下启动运行: wk run
请求示例: curl -X POST -d {} -w '\n' http://127.0.0.1:1206/v1/demo/hello/get
	`)
}
